using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class HinhAnh_DAL
    {
        private SqlConnection cn = null;
        public  HinhAnh_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(HinhAnh_DTO hinhAnh)
        {
            SqlCommand cm = new SqlCommand("tb_HinhAnh_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            //cm.Parameters.AddWithValue("@MaHinh", hinhAnh.MaHinh);
            cm.Parameters.AddWithValue("@MaMH", hinhAnh.MaMH);
            cm.Parameters.AddWithValue("@Hinh", hinhAnh.Hinh);
            cm.ExecuteNonQuery();
        }
        
        public void Update(HinhAnh_DTO hinhAnh)
        {
            SqlCommand cm = new SqlCommand("tb_HinhAnh_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinh", hinhAnh.MaHinh);
            cm.Parameters.AddWithValue("@MaMH", hinhAnh.MaMH);
            cm.Parameters.AddWithValue("@Hinh", hinhAnh.Hinh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_HinhAnh_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinh", iD);
            cm.ExecuteNonQuery();
        }
        public void Delete_MATHANG(int mamh)
        {
            SqlCommand cm = new SqlCommand("tb_HinhAnh_Delete_MATHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaMH", mamh);
            cm.ExecuteNonQuery();

        }
        public tbHinhAnhCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_HinhAnh_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbHinhAnhCollection hinhAnhCollection = new tbHinhAnhCollection();

            while (dr.Read())
            {
                HinhAnh_DTO hinhAnh = new HinhAnh_DTO();
                hinhAnh.MaHinh = int.Parse(dr.GetValue(0).ToString());
                hinhAnh.MaMH = int.Parse(dr.GetValue(1).ToString());
                hinhAnh.Hinh = dr.GetValue(2).ToString();

                hinhAnhCollection.Add(hinhAnh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhAnhCollection;
        }
    }
}
